<!-- Copyright (c) 2014 The Chromium Authors. All rights reserved.
Use of this source code is governed by a BSD-style license that can be
found in the LICENSE file. -->

<link rel="import" href="../common/cr-action.html">
<link rel="import" href="../common/cr-toolbar.html">
<link rel="import" href="cr-issue-cq-dry-run-dialog.html">
<link rel="import" href="cr-issue-patchfile.html">
<link rel="import" href="cr-try-results.html">
<link rel="import" href="../diff/cr-diff-controls.html">

<polymer-element name="cr-issue-patchset" attributes="patchset mode">
    <template>
        <link rel="stylesheet" href="../common/common.css">
        <link rel="stylesheet" href="cr-issue-patchset.css">
        <template if="{{ patchset.sourceFiles.length }}">
            <header>
                <h2>Files</h2>
                <div class="ui-actions">
                    <a is="cr-action" on-tap="{{ expandAllDiffs }}">Show diffs</a> /
                    <a is="cr-action" on-tap="{{ collapseAllDiffs }}">Hide diffs</a> /
                    <a href="/download/issue{{ patchset.issue.id }}_{{ patchset.id }}.diff" target="_blank">Raw diff</a>
                    <cr-diff-controls mode="{{ mode }}"></cr-diff-controls>
                </div>
            </header>
            <div class="files">
                <template repeat="{{ file in patchset.sourceFiles }}">
                    <cr-issue-patchfile file="{{ file }}" mode="{{ mode }}"></cr-issue-patchfile>
                </template>
            </div>
        </template>
        <template if="{{ patchset.testFiles.length }}">
            <header>
                <h2>LayoutTests</h2>
                <template if="{{ !patchset.sourceFiles.length }}">
                    <div class="ui-actions">
                        <a is="cr-action" on-tap="{{ expandAllDiffs }}">Show diffs</a> /
                        <a is="cr-action" on-tap="{{ collapseAllDiffs }}">Hide diffs</a> /
                        <a href="/download/issue{{ patchset.issue.id }}_{{ patchset.id }}.diff" target="_blank">Raw diff</a>
                        <cr-diff-controls mode="{{ mode }}"></cr-diff-controls>
                    </div>
                </template>
            </header>
            <div class="files">
                <template repeat="{{ file in patchset.testFiles }}">
                    <cr-issue-patchfile file="{{ file }}" mode="{{ mode }}"></cr-issue-patchfile>
                </template>
            </div>
        </template>
        <template if="{{ patchset.files.length && ((patchset.mostRecent && !patchset.issue.commit) || (!patchset.mostRecent && (!tryJobResults || tryJobResults.length))) }}">
            <div id="try-job-results">
                <h2>
                    Try job results
                    <template if="{{ !tryJobResults }}">
                        <img src="../images/dots16.gif" class="spinner">
                    </template>
                </h2>
                <template if="{{ tryJobResults.length }}">
                    <cr-try-results results="{{ tryJobResults }}"></cr-try-results>
                </template>
                <template if="{{ patchset.mostRecent && !patchset.issue.closed && !patchset.issue.commit }}">
                    <a class="choose-trybots" is="cr-action" on-tap="{{ handleAddMore }}">Choose try jobs</a> /
                    <a class="trigger-dry-run" is="cr-action" on-tap="{{ showDryRunDialog }}">CQ dry run</a>
                </template>
            </div>
        </template>
        <div id="dialogs"></div>
    </template>
    <script>
        Polymer({
            created: function() {
                this.patchset = null;
                this.tryJobResults = null;
            },
            showDryRunDialog: function() {
              if (this.patchset.issue.closed)
                  return;
              var dialog = document.createElement("cr-issue-cq-dry-run-dialog");
              this.showDialog(dialog);
            },
            showDialog: function(dialog) {
                this.$.dialogs.appendChild(dialog);
                dialog.addEventListener("close", function() {
                    dialog.remove();
                });
                dialog.showModal();
            },
            patchsetChanged: function(oldValue, newValue) {
                var self = this;
                var list = new TryJobResultList(this.patchset);
                list.loadResults().then(function(results) {
                    self.tryJobResults = results;
                }).catch(function(e) {
                    // TODO(esprehn): Show an error.
                    console.log(e);
                });
            },
            getFiles: function() {
                // TODO(esprehn): Use an id instead.
                return this.shadowRoot.querySelectorAll("cr-issue-patchfile").array();
            },
            expandAllDiffs: function() {
                return Promise.all(this.getFiles().map(function(file) {
                    return file.showDiff();
                }));
            },
            collapseAllDiffs: function() {
                return Promise.all(this.getFiles().map(function(file) {
                    return file.hideDiff();
                }));
            },
            handleAddMore: function() {
                this.fire("show-try-dialog");
            },
        });
    </script>
</polymer-element>
